﻿using Angela.Core;
using MVCBootStrapShell_WithTheme.DAL;
using MVCBootStrapShell_WithTheme.Filters;
using MVCBootStrapShell_WithTheme.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MVCBootStrapShell_WithTheme.Controllers
{
    public class PageController : BaseController
    {
        private SiteDataContext db = new SiteDataContext();

        //// GET: Page
        public ActionResult Index()
        {
            return View(db.Pages.ToList());
        }

        // GET: Page/Details/5
        public ActionResult Display(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Page page = db.Pages.Find(id);

            if (page == null)
            {
                return HttpNotFound();
            }
            return View(page);
        }

        // GET: Page/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Page page = db.Pages.Find(id);
            page.PageContents = db.PageContents.Where(pc => pc.PageID == id).ToList();

            if (page == null)
            {
                return HttpNotFound();
            }
            return View(page);
        }

        // GET: Page/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Page/Create
        //// Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
        //// plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        //public ActionResult Create(FormCollection collection)
        public ActionResult Create([Bind(Include = "PageID,PageType,Title,SubTitle,Keywords")] Page page)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Pages.Add(page);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(page);
            }
            catch
            {
                return View();
            }
        }

        // GET: Page/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Page page = db.Pages.Find(id);
            if (page == null)
            {
                return HttpNotFound();
            }
            return View(page);
        }

        // POST: Page/Edit/5
        [HttpPost]
        public ActionResult Edit([Bind(Include = "PageID,PageType,Title,SubTitle,PageContents,Keywords")] Page page)
        {
            if (ModelState.IsValid)
            {
                db.Entry(page).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(page);
        }

        // GET: Page/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Page page = db.Pages.Find(id);
            if (page == null)
            {
                return HttpNotFound();
            }
            return View(page);
        }

        // POST: Page/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Page page = db.Pages.Find(id);
            db.Pages.Remove(page);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        ////// GET: Page
        //public ActionResult Display()
        //{
        //    //return View();
        //    return View(db.Pages);
        //}

        public ActionResult SearchPage(string searchText)
        {
            var term = searchText.ToLower();
            List<Page> _pages = new List<Page>();

            foreach (Page page in db.Pages)
            {
                _pages.Add(page);
            }


            var result = _pages
                .Where(p =>
                    p.Title.ToLower().Contains(term) ||
                    p.SubTitle.ToLower().Contains(term) ||
                    p.Keywords.ToLower().Contains(term)
                );

            return PartialView("_SearchPage", result);
        }


        //public ActionResult Create()
        //{
        //    //var page = new Page();
        //    return View();
        //}

        ////[HttpPost]
        //public ActionResult Create(Page page)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _pages.Add(page);
        //        Success(string.Format("<b>{0}</b> was successfully added to the database.", page.Title), true);
        //        return RedirectToAction("Index");
        //    }
        //    Danger("Looks like something went wrong. Please check your form.");
        //    return View(page);
        //}
    }
}